<template>
  <div>
    <!-- 搜索栏 -->
    <el-card id="serach">
      <el-row>
        <el-col :span="20">
          <el-input
            v-model="searchModel.username"
            placeholder="用户名"
            clearable
          ></el-input>
          <el-input
            v-model="searchModel.phone"
            placeholder="手机号"
            clearable
          ></el-input>
          <el-button
            type="success"
            plain
            icon="el-icon-search"
            @click="getUserList"
            >搜索</el-button
          >
        </el-col>
        <el-col :span="4" align="right">
          <el-button
            type="success"
            plain
            icon="el-icon-plus"
            circle
            @click="openEditUi(null)"
          ></el-button>
        </el-col>
      </el-row>
    </el-card>

    <!-- 结果列表 -->
    <el-card>
      <el-table :data="userList" border style="width: 100%">
        <el-table-column label="#" width="30">
          <template slot-scope="scope">
            {{
              (searchModel.pageNo - 1) * searchModel.pageSize + 1 + scope.$index
            }}
          </template>
        </el-table-column>
        <el-table-column prop="id" label="用户id" width="180">
        </el-table-column>
        <el-table-column prop="username" label="用户名" width="180">
        </el-table-column>
        <el-table-column prop="phone" label="电话" width="180">
        </el-table-column>
        <el-table-column prop="status" label="用户状态" width="180">
          <template slot-scope="scope">
         <el-tag v-if="scope.row.status == 1">正常</el-tag>
         <el-tag type="danger" v-else>异常</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="email" label="电子邮件"> </el-table-column>
        <el-table-column label="操作" width="180">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" circle size="mini" @click="openEditUi(scope.row.id)"></el-button>
            <el-button type="danger" icon="el-icon-delete" circle size="mini" @click="openEditUi"></el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="searchModel.pageNo"
      :page-sizes="[2, 5, 10, 20]"
      :page-size="searchModel.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>

    <!-- 新增对话框 -->
    <el-dialog
      @close="clearFrom"
      :title="title"
      :visible.sync="dialogFormVisible"
    >
      <el-form :model="userform" :rules="rules" ref="userformref">
        <el-form-item
          label="用户名"
          :label-width="formLabelWidth"
          prop="username"
        >
          <el-input v-model="userform.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item v-if="userform.id == null || userform.id == undefined"
        label="登录密码" :label-width="formLabelWidth">
          <el-input v-model="userform.password" autocomplete="off" type="password"></el-input>
        </el-form-item>
        <el-form-item label="联系电话" :label-width="formLabelWidth">
          <el-input v-model="userform.phone" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="用户状态" :label-width="formLabelWidth">
          <el-switch
            v-model="userform.status"
            :active-value="1"
            :inactive-value="0"
          >
          </el-switch>
        </el-form-item>
        <el-form-item label="电子邮件" :label-width="formLabelWidth">
          <el-input v-model="userform.email" autocomplete="off"  type="email"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="success" @click="saveuser">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import roleApi from "@/api/role";
export default {
  data() {
    return {
      formLabelWidth: "130px",
      userform: {},
      searchModel: {
        pageNo: 1,
        pageSize: 10,
      },
      total: 0,
      userList: [],
      title: "",
      dialogFormVisible: false,
      rules: {
        username: [
          { required: true, message: "请输入活动名称", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getUserList();
  },
  methods: {
    openEditUi(id) {
      if (id == null) {
        this.title = "新增用户";
      }else{
        this.title = "修改用户";
        roleApi.getUserById(id).then(response =>{
          this.userform = response.data;
        })
      }
      this.dialogFormVisible = true;
    },
    getUserList() {
      roleApi.getuserList(this.searchModel).then((Response) => {
        this.userList = Response.data.rows;
        this.total = Response.data.total;
      });
    },
    handleSizeChange(pageSize) {
      this.searchModel.pageSize = pageSize;
      this.getUserList();
    },
    handleCurrentChange(pageNo) {
      this.searchModel.pageNo = pageNo;
      this.getUserList();
    },
    clearFrom() {
      this.userform = {};
      this.$refs.userformref.clearValidate();
    },
    saveuser() {
      this.$refs.userformref.validate((valid) => {
        if (valid) {
          roleApi.saveUser(this.userform).then(response => {
            this.$message({
              showClose: true,
              message: "response.message",
              type: "success",
            });
            this.dialogFormVisible = false
            this.getUserList();
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>

<style>
#serach .el-input {
  width: 200px;
  margin-right: 10px;
}
.el-dialog .el-input {
  width: 85%;
}
</style>